package com.qiantu.phone.widget.btnview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public abstract class BaseBtnView extends View implements View.OnClickListener, View.OnLongClickListener {
    private int pressKeyPosition;
    public BaseBtnView(Context context) {
        super(context);
    }

    public BaseBtnView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseBtnView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public BaseBtnView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                pressKeyPosition = clickPosition((int)event.getX(), (int)event.getY());
                if(pressKeyPosition <= 0){
                    setPressed(false);
                    return true;
                }
                invalidate();
            case MotionEvent.ACTION_MOVE:
                int eventPosition = clickPosition((int)event.getX(), (int)event.getY());
                if (eventPosition != pressKeyPosition){
                    pressKeyPosition = 0;
                    invalidate();
                    setPressed(false);
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                //没有设置点击回调时，在up时清除按下的颜色
                if(mOnBtnClickListener == null){
                    pressKeyPosition = 0;
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                pressKeyPosition = 0;
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onClick(View v) {
        if (pressKeyPosition > 0 && mOnBtnClickListener != null){
            mOnBtnClickListener.onClick(pressKeyPosition);
            pressKeyPosition = 0;
            invalidate();
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (pressKeyPosition > 0 && mOnBtnLongClickListener != null){
            mOnBtnLongClickListener.onLongClick(pressKeyPosition);
            pressKeyPosition = 0;
            invalidate();
        }
        return true;
    }

    private OnBtnClickListener mOnBtnClickListener;
    public void setOnBtnClickListener(OnBtnClickListener listener) {
        if (listener != null){
            mOnBtnClickListener = listener;
            setClickable(true);
            setOnClickListener(this);
        }
    }

    private OnBtnLongClickListener mOnBtnLongClickListener;
    public void setOnBtnLongClickListener(OnBtnLongClickListener listener){
        if (listener!=null){
            mOnBtnLongClickListener = listener;
            setOnLongClickListener(this);
        }
    }

    public abstract int clickPosition(int x, int y);

    public int getPressKeyPosition(){
        return pressKeyPosition;
    }
}
